﻿// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.ComponentModel.Composition.ReflectionModel;
using System.Linq;
using Microsoft.Internal;

namespace System.ComponentModel.Composition.AttributedModel
{
    internal class AttributedComposablePart : ReflectionComposablePart
    {
        private readonly AttributedComposablePartDefinition _definition;

        public AttributedComposablePart(AttributedComposablePartDefinition definition)
        {
            Requires.NotNull(definition, "definition");

            _definition = definition;
        }

        public AttributedComposablePart(object attributedPart)
            : base(attributedPart)
        {
            // If given an instance then we want to pass the default composition options because we treat it as a shared part
            // TODO: ICompositionElement Give this def an origin indicating that it was added directly to the MutableExportProvider.
            _definition = new AttributedComposablePartDefinition(attributedPart.GetType(), true, CompositionOptionsAttribute.Shared, (ICompositionElement)null);
        }

        public override ReflectionComposablePartDefinition Definition
        {
            get { return _definition; }
        }

        protected override ImportingItem GetImportingItem(ImportDefinition definition)
        {
            AttributedImportDefinition attributedDefinition = definition as AttributedImportDefinition;
            if (attributedDefinition != null)
            {
                return attributedDefinition.ToImportingItem();
            }

            // Don't recognize it
            return null;
        }

        protected override ExportingMember GetExportingMember(ExportDefinition definition)
        {
            AttributedMemberExportDefinition attributedDefinition = definition as AttributedMemberExportDefinition;
            if (attributedDefinition != null)
            {
                return attributedDefinition.ToExportingMember();
            }

            // Don't recognize it
            return null;
        }
    }
}
